package cn.iocoder.yudao.module.forward.service.firm;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.forward.controller.admin.firm.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.firm.FirmDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.forward.dal.mysql.firm.FirmMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.*;

/**
 * 厂商 Service 实现类
 *
 * @author zhang
 */
@Service
@Validated
public class FirmServiceImpl implements FirmService {

    @Resource
    private FirmMapper firmMapper;

    @Override
    public Integer createFirm(FirmSaveReqVO createReqVO) {
        // 插入
        FirmDO firm = BeanUtils.toBean(createReqVO, FirmDO.class);
        firmMapper.insert(firm);
        // 返回
        return firm.getId();
    }

    @Override
    public void updateFirm(FirmSaveReqVO updateReqVO) {
        // 校验存在
        validateFirmExists(updateReqVO.getId());
        // 更新
        FirmDO updateObj = BeanUtils.toBean(updateReqVO, FirmDO.class);
        firmMapper.updateById(updateObj);
    }

    @Override
    public void deleteFirm(Integer id) {
        // 校验存在
        validateFirmExists(id);
        // 删除
        firmMapper.deleteById(id);
    }

    private void validateFirmExists(Integer id) {
        if (firmMapper.selectById(id) == null) {
            throw exception(FIRM_NOT_EXISTS);
        }
    }

    @Override
    public FirmDO getFirm(Integer id) {
        return firmMapper.selectById(id);
    }

    @Override
    public PageResult<FirmDO> getFirmPage(FirmPageReqVO pageReqVO) {
        return firmMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FirmDO> getAllFirm() {
        return firmMapper.selectList("enable",1);
    }

}